package com.tanhua.server.controller;

import com.tanhua.model.domain.BaseResult;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;


@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 保存用户信息
     * UserInfo
     * 请求头中携带token
     */
    @PostMapping("/loginReginfo")
    public ResponseEntity loginReginfo(@RequestBody UserInfo userInfo,
                                       @RequestHeader("Authorization") String token) {
        userInfo.setId(UserHolder.getUserId());
        //3、调用service
        userInfoService.save(userInfo);
        UserInfo userById = userInfoService.getUserById(UserHolder.getUserId());
        return ResponseEntity.ok(BaseResult.builder().data(userById).build());
    }

    @PostMapping("/loginReginfo/head")
    public ResponseEntity head(MultipartFile headPhoto,
                                     @RequestHeader("Authorization") String token) throws IOException {
        String headAddress = userInfoService.updateHead(headPhoto, UserHolder.getUserId());
        if(headAddress==null){
            return ResponseEntity.ok(BaseResult.builder().code(1).msg("上次失败").build());
        }
        return ResponseEntity.ok(BaseResult.builder().data(headAddress).build());

    }

}
